﻿using System;
using System.Collections.Generic;
using RentalSystem.Data.EF.Core;

namespace RentalSystem.Data.EF.Dao
{
    public class EFFilesDao : IFilesDao
    {
        #region Queries

        private static string GetQuery;
        private static string GetByPathQuery;

        #endregion

        private EFManager<Files> manager;

        public EFFilesDao(RentalDatabaseEntities repository)
        {
            GetByPathQuery = "SELECT VALUE f FROM Files AS f WHERE f.FileUrl = @Path";
            GetQuery = "SELECT VALUE f FROM Files AS f WHERE f.FileID = @Id";

            this.manager = new EFManager<Files>(repository);
        }
        
        #region IDao1Key

        public Files Add(Files entity)
        {
            manager.Database.AddToFiles(entity);
            manager.Save();
            return entity;
        }

        public Files Get(int id)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@Id", id)
                };

            var files = manager.SQLQueryToEntity(GetQuery, parameters);

            foreach (Files file in files)
            {
                return file;
            }

            return null;
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public void Delete(Files entity)
        {
            throw new NotImplementedException();
        }

        public void Update(Files entity, uint fields)
        {
            throw new NotImplementedException();
        }

        public List<Files> List()
        {
            throw new NotImplementedException();
        }

        #endregion

        public Files GetByPath(string path)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@Path", path)
                };

            var files = manager.SQLQueryToEntity(GetByPathQuery, parameters);

            foreach (Files file in files)
            {
                return file;
            }

            return null;
        }

        public void DeleteByPath(string path)
        {
            Files file = GetByPath(path);
            if (file != null)
            {
                manager.Database.Files.DeleteObject(file);
                manager.Save();
            }
        }
    }
}
